package leetcode.primary;

public class T73 {
    public void setZeroes(int[][] matrix) {
        boolean x_zero = false;
        boolean y_zero = false;
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[0].length; j++) {
                if (matrix[i][j]==0){
                    matrix[i][0] = 0;
                    matrix[0][j] = 0;
                    if(i==0){
                        x_zero = true;
                    }
                    if (j==0){
                        y_zero = true;
                    }
                }

            }
        }

        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[0].length; j++) {
                System.out.print(matrix[i][j]+" ");
            }
            System.out.println();
        }

        System.out.println("x_zero = "+x_zero+",y_zero="+y_zero);
        System.out.println("-----------------------");

        for (int i = matrix.length-1; i>=0; i--) {
            for (int j = matrix[0].length-1; j >=0; j--) {
                if (i==0||j==0){
                    if (i==0 && x_zero ){
                        matrix[i][j] = 0;
                    }
                    if (j==0 && y_zero){
                        matrix[i][j] = 0;
                    }
                }else if (matrix[i][0]==0 || matrix[0][j]==0){
                    matrix[i][j] = 0;
                }
            }
        }
    }

    public static void main(String[] args) {
        T73 test = new T73();
        int[][] matrix = new int[][]{{1,2,3,4},{5,0,7,8},{0,10,11,12},{13,14,15,0}};
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[0].length; j++) {
                System.out.print(matrix[i][j]+" ");
            }
            System.out.println();
        }
        System.out.println("-------------");
        test.setZeroes(matrix);
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[0].length; j++) {
                System.out.print(matrix[i][j]+" ");
            }
            System.out.println();
        }
    }
}
